package disjunction.core
{
	import disjunction.core.geom.Vector2;
	
	import flash.geom.Rectangle;
	
	public class Camera2 implements ISpatial2D
	{
		private var _position:Vector2;
		private var _scale:Vector2;
		private var _rotation:Number;
		
		public function get position():Vector2
		{
			return _position;//.clone();
		}
		
		public function set position(value:Vector2):void
		{
			//non-primitive, so make sure to copy the object's values
			//(as the external ref could affect this object)
			_position.x = value.x;
			_position.y = value.y;
		}
		
		public function get scale():Vector2
		{
			return _scale;//.clone();
		}
		
		public function set scale(value:Vector2):void
		{
			//non-primitive, so make sure to copy the object's values
			//(as the external ref could affect this object)
			_scale.x = value.x;
			_scale.y = value.y;
		}
		
		public function get rotation():Number
		{
			return _rotation;
		}
		
		public function set rotation(value:Number):void
		{
			_rotation = value;
		}
		
		public function get bounds():Rectangle
		{
			return new Rectangle(); //no dimensions
		}
		
		public function update(entityViews:Array):void
		{
			//NOTE each strategy modifies the entity list directly!
			//(in preparation for the next, and ultimately for the renderer if any)
			
			//transform
			//2d camera needs to transform using position and rotation
			//this strategy will expect entities as ISpatial2's
			if (transformer)
				transformer.transform(entityViews, position, rotation);
			//else
				//return entityViews;//.clone();
			/*
			//one such strat is:
			var model:ISpatial2;
			for each (var view:AbstractEntityView in entityViews)
			{
			model = view.model as ISpatial2;
			(view as ISpatial2).position.x = model.position.x;
			(view as ISpatial2).position.y = model.position.y;
			}
			*/
			
			//depth sort
			//2d camera needs to sort using position and rotation, with already transformed entities
			//...really? Will a 2D camera ever change perspective? Does rotation affect depth sorting,
			//if you're always looking along the z-axis? not sure rotation is needed here.
			if (sorter != null)
				entityViews = sorter.sort(entityViews, position);//, rotation);
			
			//one such strategy is:
			//entityViews.sortOn("z", Array.NUMERIC);
			
			
			//2d camera needs to cull using position and rotation and camera volume/dimensions, with already sorted entities
			//cull
			//NB this may be slightly tough since the bounds will not be
			//axis-aligned if the box is rotated. Still 2D vector math though,
			//if the cam volume is maintained as a cuboid, i.e. the view is orthogonal.
			//(frustum == non-orthogonal)
			if (culler != null)
				culler.cull(entityViews, position, rotation, bounds);		
		
		}
	}
}